<?php

namespace App\Admin\Controllers\Game;

use App\Admin\Constant\GameConst;
use App\Admin\Controllers\Controller;
use App\Admin\Filters\Game\GameCategoryFilter;
use App\Admin\Models\Game\GameCategory;
use App\Admin\Requests\Game\GameCategoryRequest;
use App\Admin\Resources\Game\GameCategoryResource;
use Illuminate\Http\Request;

class GameCategoryController extends Controller
{
    public function index(GameCategoryFilter $filter)
    {

         $gameCategorys = GameCategory::query()
                        ->filter($filter)
                        ->where('type', 2)
                        ->select('id', 'name', 'parent_id')
                        ->where('is_delete',2)
                        ->get();
          $resetData = [];
          $res = $this->getChildren($gameCategorys);
        if(\request()->input('dataType')==1){
         array_unshift($res,[
                'id'=>0,
                'name'=>'--作为一级分类'
            ]);
        }

//       $resetData['meta'] = [
//           'total'=>   $gameCategorys->total(),
//           'current_page'=>$gameCategorys->currentPage(),


//            'per_page'=>$gameCategorys->perPage(),
//           'last_page'=>$gameCategorys->lastPage()
//       ];
    return $this->ok(GameCategoryResource::collection($res));
    }


    function getChildren($list, $pid = 0)
    {
        $arr = array();
        foreach ($list as $key => $value) {
            if ($value['parent_id'] == $pid) {
                $value['children'] =
             $s = $this->getChildren($list, $value['id']);
                if(!is_array($s) || empty($s)){
                    unset($value['children']);
                }
                $arr[] = $value;
            }
        }
        return $arr;
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(GameCategoryRequest $request)
    {
        $inputs = $request->validated();
        $data = $request->input();
        $s = GameCategory::query()->where(['name'=>$data['name']])->get();
        if(!empty($s->toArray())){

            return $this->errorMessage(GameConst::GAME__EXIST);
        }
//        var_dump($data);exit();
        $gameCategory = GameCategory::query()->insert($data);

        return $this->ok(GameConst::GAME_GROUP__ADD_SUCCESS);
    }

    public function edit(Request $request, GameCategory $gameCategory)
    {
        return $this->ok(GameCategoryResource::make($gameCategory));
    }

    public function update($id,GameCategoryRequest $request)
    {
//        $inputs = $request->validated();
       $data =  GameCategory::query()->where('id',$id)->update($request->input());
       if(!$data){
           return $this->errorMessage(GameConst::GAME_GROUP_EDIT_FAIL);
       }
        return $this->ok(GameConst::GAME_GROUP_EDIT_SUCCESS);
    }

    public function destroy($id)
    {
        $data = GameCategory::query()->where('id',$id)->update(['is_delete'=>1,'delete_time'=>time()]);
        if(!$data){
            return $this->errorMessage(GameConst::GAME_FAIL);
        }
        return $this->ok(GameConst::GAME_DELETE_SUCCESS);
    }
}
